<?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) wangyt <wangtom365@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace App\Models\Dict;

use Cache;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use App\Models\BaseModel;

class DictSentencesModel extends BaseModel
{
    /**
     * Constructor.
     */
    public function __construct()
    {
        parent::__construct();
        
        // $this->table = 'dict_sentences';
        $this->table = \Config::get('site-tables.tablePrefix') . \Config::get('site-tables.tableName.dict_sentences');
    }
 
    /**
     * 根据分类获取每个分类的文章.
     *
     * @param mixed $where
     *
     * @return static
     */
    public function getListGroupByCategory($where = [])
    {
        $cacheKey = $this->formatCacheKey(__FUNCTION__, func_get_args());
        $minutes = 1;
        $result = Cache::remember($cacheKey, $minutes, function () use ($where, $cacheKey, $minutes) {
            $data = [];
            $data['count'] = $this->getListCountGroupByCategory($where);
            $data['list'] = $this->getListDataGroupByCategory($where);

            if ($data) {
                Cache::put($cacheKey, $data, $minutes);
            }
        });

        return $result;
    }

    /**
     * 获取每个分类下的文章数量.
     *
     * @param mixed $where
     *
     * @return mixed
     */
    public function getListCountGroupByCategory($where)
    {
        $condition = [];
        if (isset($where['category']) && $where['category']) {
            $condition['category'] = $where['category'];
        }
        // if (isset($where['industry']) && $where['industry']) {
        //     $condition['industry'] = $where['industry'];
        // }
        // $condition['status'] = 1;

        $result = self::select(DB::raw('category, count(*) AS total'))
            ->where($condition)
            ->groupBy('category')
            ->get();

        if ($result) {
            $result = $result->mapWithKeys(function ($item) {
                return [$item['category'] => $item['total']];
            })->toArray();
        }

        return $result;
    }

    private function getListDataGroupByCategory($where)
    {
        $condition = [];
        if (isset($where['category']) && $where['category']) {
            $condition['category'] = $where['category'];
        }
        // if (isset($where['industry']) && $where['industry']) {
        //     $condition['industry'] = $where['industry'];
        // }
        // $condition['status'] = 1;

        $result = collect($categoryResult)->map(function ($item, $key) {
            $where = collect([
                'select' => 'id, title, category',
                'category' => $key,
            ]);

            $res = $this->getList($where);

            if ($res->isEmpty()) {
                return false;
            }

            return $res->sortByDesc('id')->toArray();
        });

        return $result;
    }
}
